import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../utils/constant.dart';
import '../utils/toast_util.dart';

class MySearchAppBar extends AppBar {
  final GlobalKey<NavigatorState> navigatorKey =
      new GlobalKey<NavigatorState>();

  MySearchAppBar(
      {
        required  Key key,
        required BuildContext context,
        defaultText='',
      hasLeading = false,
      callBack = Function})
      : super(
            bottom: PreferredSize(
                preferredSize: const Size.fromHeight(0),
                child: Container(
                  color: const Color(0xFFDDDDDD),
                  width: double.infinity,
                  height: 0.5,
                )),

            title: _top(context,callBack,defaultText),
            elevation: 0,
            leading:   IconButton(
              icon: Icon(Icons.arrow_back, color: Colors.black),
              onPressed: () => {
                callBack('back')
              },
            ),
            backgroundColor: Colors.white,
            centerTitle: false);

}
Widget _top(context,callBack,String defaultText) {
  print('defaultTextdefaultText=='+defaultText);
  TextEditingController textEditingController = TextEditingController();
  var content = defaultText.obs;
  textEditingController.text = content.value;
  return Row(
    children: [

      Expanded(
          flex: 1,
          child: Container(
            height: 32.h,
            decoration: BoxDecoration(
                color: const Color(0xFFF5F5F5),
                borderRadius: BorderRadius.circular(17.r)),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                SizedBox(
                  width: 18.w,
                ),
                Image.asset(
                  '${Constant.IMG_BASE}home_search.png',
                  width: 14.w,
                  height: 14.h,
                ),
                SizedBox(
                  width: 8.w,
                ),
                Container(
                    width: MediaQuery.of(context).size.width - 200.w,
                    child: TextField(

                      controller: textEditingController,
                      decoration: InputDecoration(

                        border: const OutlineInputBorder(borderSide: BorderSide.none),
                        contentPadding: EdgeInsets.symmetric(vertical: 0),
                        counterText: "",
                        hintText: '请输入内容',
                        labelStyle: TextStyle(
                            color: Color(0xFFB3B3C6), fontSize: 14.sp),
                        // contentPadding: EdgeInsets.only(left: 0, top: 14, bottom: 16),
                        hintStyle: TextStyle(
                            color: Color(0xFFB3B3C6), fontSize: 14.sp),
                      ),
                      onChanged: (str) {
                        print('onChangedonChanged=='+str);
                          content.value = str;
                        callBack(str);
                      },
                    ))
              ],
            ),
          )),
      InkWell(
          onTap: () {
            print('onChangedonChanged==='+content.value);
            if (   content.value.isEmpty) {
              ToastUtil.show("请输入搜索内容");
              return;
            }
            callBack('goSearch');
          },
          child: Container(
            margin: EdgeInsets.only(left: 16.w, right: 21.w),
            child: Text(
              '搜索',
              style:
              TextStyle(color: const Color(0xFF333333), fontSize: 13.sp),
            ),
          ))
    ],
  );
}
